package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealOverViewVO;
import com.sky.vo.SetmealVO;
import org.apache.http.client.HttpClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 山顶洞人郑某
 * @className SetmealServiceImpl.java
 * @date 2024 01 21
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 添加套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setmealMapper.insert(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 根据id查询套餐信息
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public SetmealVO getById(Long id) {

        //先查询出套餐信息
        Setmeal setmeal =  setmealMapper.getById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //再通过套餐id查询套餐菜品关联表中所有套餐关联菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        //再查询分类名称
        String categoryName =  categoryMapper.getNamebyId(setmealVO.getCategoryId());
        setmealVO.setCategoryName(categoryName);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        //先将套餐关联菜品信息删除
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //再将套餐信息更新
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //再将套餐更行的菜品信息添加到套餐菜品关联表
        setmealDishMapper.insert(setmealDTO.getSetmealDishes());
    }

    /**
     * 商品起售和停售
     * @param status
     */
    @Override
    public void status(Integer status, Long id) {
        setmealMapper.status(status,id);
    }

    /**
     * 套餐批量删除
     *
     * @param ids
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {

        //先将套餐关联菜品信息表中信息删除
        setmealDishMapper.deleteBySetmealIds(ids);
        //再将套餐删除
        setmealMapper.deleteByIds(ids);
    }

    /**
     * 根据分类id查询所有套餐
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> list(Long categoryId) {
        return setmealMapper.list(categoryId,StatusConstant.ENABLE);
    }

    /**
     * 停售和起售套餐数量查询
     *
     * @return
     */
    @Override
    public SetmealOverViewVO getoverviewOrders() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(setmealMapper.getSumByStatus(StatusConstant.ENABLE));
        setmealOverViewVO.setDiscontinued(setmealMapper.getSumByStatus(StatusConstant.DISABLE));
        return setmealOverViewVO;
    }
}
